﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Resource;

namespace MainUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool cardEnabled
        {
            get
            {
                return this.nameLabel.Enabled;
            }
            set
            {
                this.nameLabel.Enabled = value;
                this.orderCheckBox.Enabled = value;
                this.symbolCheckBox.Enabled = value;
                this.ramCheckBox.Enabled = value;
                this.valenceCheckBox.Enabled = value;
                this.orderNumericUpDown.Enabled = value;
                this.symbolTextBox.Enabled = value;
                this.ramNumericUpDown.Enabled = value;
                this.valenceTextBox.Enabled = value;
                this.submitButton.Enabled = value;
                this.skipButton.Enabled = value;
                if (value == false)
                {
                    timer2.Stop();
                }
                else
                {
                    timer2.Start();
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
        }

        private bool isChangedByDev = false;
        private void symbolTextBox_TextChanged(object sender, EventArgs e)
        {
            if (isChangedByDev)
                return;
            int index = symbolTextBox.SelectionStart;
            char[] strarray = symbolTextBox.Text.ToLower().ToCharArray();
            try
            {
                strarray[0] = strarray[0].ToString().ToUpper().ToCharArray()[0];
            }
            catch { }
            isChangedByDev = true;
            symbolTextBox.Text = new string(strarray);
            symbolTextBox.SelectionStart = index;
            isChangedByDev = false;
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            cardEnabled = true;
            startButton.Enabled = false;
            stopButton.Enabled = true;
            createNewCard();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                submitButton.PerformClick();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            cardEnabled = false;
            startButton.Enabled = true;
            stopButton.Enabled = false;
            //重置卡片
            elements = null;
            elementCount = 0;
            isElementsLoaded = false;
            isRamEnabled = true;
            isValenceEnabled = true;
        }

        private Resource.Elements.Element[] elements;
        private int elementCount = 0;
        private bool isElementsLoaded = false;
        private bool isOrderEnabled = false;
        private bool isSymbolEnabled = true;
        private bool isRamEnabled = true;
        private bool isValenceEnabled = true;
        private bool ramSupport = true;
        private bool valenceSupport = true;

        private string order;
        private string name;
        private string symbol;
        private string ram;
        private string valence;
        /// <summary>
        /// 创建新的卡片
        /// </summary>
        private void createNewCard()
        {
            if(!isElementsLoaded)
            {
                elements = new Resource.Elements.XElements().Elements;
            }
            if (elementCount <= elements.Length - 1)
            {
                order = elements[elementCount].Order;
                name = elements[elementCount].Name;
                symbol = elements[elementCount].Symbol;
                try
                {
                    ram = elements[elementCount].RelativeAtomicMass;
                    ramSupport = true;
                }
                catch (NotSupportedException)
                {
                    ramSupport = false;
                }
                try
                {
                    valence = elements[elementCount].Valence;
                    valenceSupport = true;
                }
                catch (NotSupportedException)
                {
                    valenceSupport = false;
                }
            }
            else
            {
                elementCount = 0;
                createNewCard();
            }
            cardInit();
            symbolTextBox.Text = "";
            valenceTextBox.Text = "";
        }

        private void cardInit()
        {
            if (!ramSupport)
                ramNumericUpDown.Enabled = false;
            else
                ramNumericUpDown.Enabled = true;
            if (!valenceSupport)
                valenceTextBox.Enabled = false;
            else
                valenceTextBox.Enabled = true;
            nameLabel.Text = name;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            if (isOrderEnabled)
            {
                if (!(orderNumericUpDown.Value == Convert.ToDecimal(order)))
                {
                    madeMistake(Mistake.Order, elementCount);
                    return;
                }
            }
            if (isSymbolEnabled)
            {
                if (!symbolTextBox.Text.Equals(symbol))
                {
                    madeMistake(Mistake.Symbol, elementCount);
                    return;
                }
            }
            if (isRamEnabled)
            {
                if (!(ramNumericUpDown.Value == Convert.ToDecimal(ram)))
                {
                    madeMistake(Mistake.RAM, elementCount);
                    return;
                }
            }
            if (isValenceEnabled)
            {
                if (!valenceTextBox.Text.Equals(valence))
                {
                    madeMistake(Mistake.Valence, elementCount);
                    return;
                }
            }
            elementCount++;
            createNewCard();
        }

        private void skipButton_Click(object sender, EventArgs e)
        {
            madeMistake(Mistake.Skip,0);
            createNewCard();
        }

        private enum Mistake : byte { Order,Symbol,RAM,Valence,Skip}
        private void madeMistake(Mistake k,int index)
        {
            if (!(k == Mistake.Skip))
                MessageBox.Show("错误,正确答案是\n" + elements[elementCount]);
        }

        private void orderCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            switch (orderCheckBox.CheckState)
            {
                case CheckState.Checked:
                    isOrderEnabled = true;
                    break;
                case CheckState.Unchecked:
                    isOrderEnabled = false;
                    break;
            }
        }

        private void symbolCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            switch (symbolCheckBox.CheckState)
            {
                case CheckState.Checked:
                    isSymbolEnabled = true;
                    break;
                case CheckState.Unchecked:
                    isSymbolEnabled = false;
                    break;
            }
        }

        private void ramCheckBox_CheckStateChanged(object sender, EventArgs e)
        {

            switch (ramCheckBox.CheckState)
            {
                case CheckState.Checked:
                    isRamEnabled = true;
                    break;
                case CheckState.Unchecked:
                    isRamEnabled = false;
                    break;
            }
        }

        private void valenceCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            switch (valenceCheckBox.CheckState)
            {
                case CheckState.Checked:
                    isValenceEnabled = true;
                    break;
                case CheckState.Unchecked:
                    isValenceEnabled = false;
                    break;
            }
        }
    }
}
